"""


测试

1确认 url
2post请求 确认data参数

"""
import json

import requests
# from jsonpath import jsonpath
# word=input("请输入内容：")
# fanyiurl = 'https://fanyi.baidu.com/v2transapi?from=zh&to=en'
# # 请求头
# headers={
#     "X-Requested-With": "XMLHttpRequest",
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36',
#     "Referer": "https://fanyi.baidu.com/translate?aldtype=16047&query=%E6%B5%8B%E8%AF%95%0D%0A&keyfrom=baidu&smartresult=dict&lang=auto2zh",
#     'Cookie': 'PSTM=1647264351; BAIDUID=9CE7556020BF8320993AF17CEC6B290F:FG=1; BIDUPSID=CBBA598AB2CF72004A27DF77A269EBEF; BDUSS=nNDdE1lMHR1LXFmaXdxeWN3YTVjVGY4QnpzMjJNRTc1UzdrdWpEQVRRb2FybGRpRVFBQUFBJCQAAAAAAAAAAAEAAABZilOrAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABohMGIaITBiM; BDUSS_BFESS=nNDdE1lMHR1LXFmaXdxeWN3YTVjVGY4QnpzMjJNRTc1UzdrdWpEQVRRb2FybGRpRVFBQUFBJCQAAAAAAAAAAAEAAABZilOrAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABohMGIaITBiM; BCLID_BFESS=7469364331890617215; BDSFRCVID_BFESS=xZFOJexroG0ljrcDVczbKknvVeKKvV3TDYLtOwXPsp3LGJLVgWj3EG0PtoaGdu_-ox8EogKKXgOTHw0F_2uxOjjg8UtVJeC6EG0Ptf8g0M5; BAIDUID_BFESS=130ACC0776D268511E3577BDEEC6770F:FG=1; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BDRCVFR[aGQGCXkfc5Y]=mk3SLVN4HKm; H_PS_PSSID=; delPer=0; PSINO=6; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; BA_HECTOR=802h8ha48h2k8g24dk1h5vvoo0r; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1650451292,1650458410; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1650458410; ab_sr=1.0.1_MjliYzg1MzBlMmFkYWRhY2JkZTc2NWE1NjZiMWNlMTMzMjM3YjZhNDFkN2JkMmZhMTY2M2Q0MTJjMDdmODk0NjAyZjg2M2I1MGRhNzU4MzNlNjAzMzk4NGE4ZGRjMDdiMzkyNTdkOWM3NGQ0MDVlYjZhNjUwMGZmMmZhYWViZmQwYWUxMjVjY2U0MDQ3NTMyNDQxYzY5YmIzMzRiZTNlZjFhMjJlYTU5ZDgzODIyMGYwY2I5MzM4NjdlZDQzYTVl'
# }
# # 表单
# data = {
#     "from": "zh",
#     "to": "en",
#     "query": word,
#     "transtype": "realtime",
#     "simple_means_flag": "3",
#     "sign": "706553.926920",
#     "token": "5eb645bc882db293a5b50a824fd871f0",
#     "domain": "common"
# }
# response=requests.post(url=fanyiurl,headers=headers,data=data)
# data=response.content.decode()
# print(data)
# datas=json.loads(data)
#
# neirong=jsonpath(datas,"$..dst")
# print(neirong)



"""做参数对比 找不同 
"from": "zh",
"to": "en",
"query": word,
"transtype": "realtime",
"simple_means_flag": "3",
"sign": "706553.926920",
"token": "5eb645bc882db293a5b50a824fd871f0",
"domain": "common"

from: zh
to: en
query: 你好
transtype: realtime
simple_means_flag: 3
sign: 232427.485594                   * 
token: 5eb645bc882db293a5b50a824fd871f0
domain: common
只有 sign变化 啦 因此要去寻找 它生成的位置  
 I(e) 是 函数的调用    e参数  翻译内容
 i==js里面的e 
 "232427.485594"
"""

from jsonpath import jsonpath
import execjs
# 1读取js文件
word=input("请输入内容：")
with open("初识NLTK.py.js","r",encoding="utf-8")as file1:
    jsdata=file1.read()

# 2通过将读取出来的 js代码 传递给 execjs的compile方法  返回一个 执行器对象
exe=execjs.compile(jsdata)
# 3通过执行器对象执行 js代码里面的call函数   call方法的第二个参数 是传递给 方法的参数
result=exe.call("get",word)
# 4得到函数抛出的返回值
print(result)

fanyiurl = 'https://fanyi.baidu.com/v2transapi?from=zh&to=en'
# 请求头
headers={
    "X-Requested-With": "XMLHttpRequest",
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36',
    "Referer": "https://fanyi.baidu.com/translate?aldtype=16047&query=%E6%B5%8B%E8%AF%95%0D%0A&keyfrom=baidu&smartresult=dict&lang=auto2zh",
    'Cookie': 'PSTM=1647264351; BAIDUID=9CE7556020BF8320993AF17CEC6B290F:FG=1; BIDUPSID=CBBA598AB2CF72004A27DF77A269EBEF; BDUSS=nNDdE1lMHR1LXFmaXdxeWN3YTVjVGY4QnpzMjJNRTc1UzdrdWpEQVRRb2FybGRpRVFBQUFBJCQAAAAAAAAAAAEAAABZilOrAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABohMGIaITBiM; BDUSS_BFESS=nNDdE1lMHR1LXFmaXdxeWN3YTVjVGY4QnpzMjJNRTc1UzdrdWpEQVRRb2FybGRpRVFBQUFBJCQAAAAAAAAAAAEAAABZilOrAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABohMGIaITBiM; BCLID_BFESS=7469364331890617215; BDSFRCVID_BFESS=xZFOJexroG0ljrcDVczbKknvVeKKvV3TDYLtOwXPsp3LGJLVgWj3EG0PtoaGdu_-ox8EogKKXgOTHw0F_2uxOjjg8UtVJeC6EG0Ptf8g0M5; BAIDUID_BFESS=130ACC0776D268511E3577BDEEC6770F:FG=1; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BDRCVFR[aGQGCXkfc5Y]=mk3SLVN4HKm; H_PS_PSSID=; delPer=0; PSINO=6; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; BA_HECTOR=802h8ha48h2k8g24dk1h5vvoo0r; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1650451292,1650458410; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1650458410; ab_sr=1.0.1_MjliYzg1MzBlMmFkYWRhY2JkZTc2NWE1NjZiMWNlMTMzMjM3YjZhNDFkN2JkMmZhMTY2M2Q0MTJjMDdmODk0NjAyZjg2M2I1MGRhNzU4MzNlNjAzMzk4NGE4ZGRjMDdiMzkyNTdkOWM3NGQ0MDVlYjZhNjUwMGZmMmZhYWViZmQwYWUxMjVjY2U0MDQ3NTMyNDQxYzY5YmIzMzRiZTNlZjFhMjJlYTU5ZDgzODIyMGYwY2I5MzM4NjdlZDQzYTVl'
}
# 表单
data = {
    "from": "zh",
    "to": "en",
    "query": word,
    "transtype": "realtime",
    "simple_means_flag": "3",
    "sign": result,
    "token": "5eb645bc882db293a5b50a824fd871f0",
    "domain": "common"
}
response=requests.post(url=fanyiurl,headers=headers,data=data)
data=response.content.decode()
datas=json.loads(data)
# 提取方法
neirong=jsonpath(datas,"$..dst")
print(neirong)
"""
小tips
1.缺啥扣啥

    i is not defined  遇到这种 就要分析js代码
    i = window[l]
    l=? 'gtk'  
    i=window['gtk']
    
    最后发现 |i="320305.131321201" 写死的 
    
    n is not defined  
    找到n 进入n定义的位置 就把n 复制到 js代码里面  执行 
2 window is not defined 
直接在js文件 顶部声明 window ={}  或者window =global;
或者window 权重 最高的属性 所有位置都能调用  

"""
